package com.sokal.easycare.modules.kpoint.web.activity;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.sokal.easycare.common.config.Global;
import com.sokal.easycare.common.persistence.Page;
import com.sokal.easycare.common.utils.StringUtils;
import com.sokal.easycare.common.web.BaseController;
import com.sokal.easycare.modules.kpoint.entity.activity.KPointActivity;
import com.sokal.easycare.modules.kpoint.service.activity.KPointActivityService;

/**
 * 体验店活动Controller
 * @author lihaipei
 * @version 2017-03-18
 */
@Controller
@RequestMapping(value = "${adminPath}/kpoint/activity/kPointActivity")
public class KPointActivityController extends BaseController {

	@Autowired
	private KPointActivityService kPointActivityService;
	
	@ModelAttribute
	public KPointActivity get(@RequestParam(required=false) String id) {
		KPointActivity entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = kPointActivityService.get(id);
		}
		if (entity == null){
			entity = new KPointActivity();
		}
		return entity;
	}
	
	@RequiresPermissions("kpoint:activity:kPointActivity:view")
	@RequestMapping(value = {"list", ""})
	public String list(KPointActivity kPointActivity, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<KPointActivity> page = kPointActivityService.findPage(new Page<KPointActivity>(request, response), kPointActivity); 
		model.addAttribute("page", page);
		model.addAttribute("kPointActivity", kPointActivity);
		return "modules/kpoint/activity/kPointActivityList";
	}

	@RequiresPermissions("kpoint:activity:kPointActivity:view")
	@RequestMapping(value = "form")
	public String form(KPointActivity kPointActivity, Model model) {
		model.addAttribute("kPointActivity", kPointActivity);
		return "modules/kpoint/activity/kPointActivityForm";
	}

	@RequiresPermissions("kpoint:activity:kPointActivity:edit")
	@RequestMapping(value = "save")
	public String save(@RequestParam(value = "activityFile") MultipartFile activityFile, KPointActivity kPointActivity, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, kPointActivity)){
			return form(kPointActivity, model);
		}
		kPointActivityService.save(activityFile, kPointActivity);
		addMessage(redirectAttributes, "保存体验店活动信息成功");
		return "redirect:"+Global.getAdminPath()+"/kpoint/activity/kPointActivity/?repage";
	}
	
	@RequiresPermissions("kpoint:activity:kPointActivity:edit")
	@RequestMapping(value = "delete")
	public String delete(KPointActivity kPointActivity, RedirectAttributes redirectAttributes) {
		kPointActivityService.delete(kPointActivity);
		addMessage(redirectAttributes, "删除体验店活动信息成功");
		return "redirect:"+Global.getAdminPath()+"/kpoint/activity/kPointActivity/?repage";
	}

	@RequiresPermissions("user")
	@ResponseBody
	@RequestMapping(value = "treeData")
	public List<Map<String, Object>> treeData(@RequestParam(required=false) String extId, HttpServletResponse response) {
		List<Map<String, Object>> mapList = Lists.newArrayList();
		List<KPointActivity> list = kPointActivityService.findList(new KPointActivity());
		for (int i=0; i<list.size(); i++){
			KPointActivity e = list.get(i);
			if (StringUtils.isBlank(extId) || (extId!=null && !extId.equals(e.getId()))){
				Map<String, Object> map = Maps.newHashMap();
				map.put("id", e.getId());
				map.put("name", e.getTitle());
				mapList.add(map);
			}
		}
		return mapList;
	}

}